package fourfaith.com.ff_sl21_app;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import fourfaith.com.ff_sl21_app.utils.Constants;
import fourfaith.com.ff_sl21_app.utils.JsonRequest;
import fourfaith.com.ff_sl21_app.utils.RequestQueueSingleton;
import fourfaith.com.ff_sl21_app.utils.ResponseResult;

public class NoticeContentActivity extends AppCompatActivity {

    private Context context;
    private ImageView back;

    private TextView content,time,title;

    private HashMap<String,Objects> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_content);

        data = (HashMap) getIntent().getSerializableExtra("dataMap");

        checkIsRead(data);
        initView();
        initData();
    }

    /**
     * 检测是否已读，未读则发送阅读状态
     * @param data
     */
    private void checkIsRead(HashMap<String, Objects> data) {

        int isRead = Integer.parseInt(String.valueOf(data.get("isread")));

        if(isRead == 0){
            //未读

            Map<String,String> params = new HashMap<>();

            String id = String.valueOf(data.get("id"));
            String userId = Constants.getUserId();

            params.put("userId",userId);
            params.put("announceId",id);

            RequestQueue mQueue = RequestQueueSingleton.getInstance(context).getRequestQueue();
            JsonRequest<ResponseResult> jsonRequest = new JsonRequest<>(Request.Method.POST, Constants.announceRead, ResponseResult.class,
                    new Response.Listener<ResponseResult>() {
                        @Override
                        public void onResponse(ResponseResult responseResult) {}
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }

            },params);
            mQueue.add(jsonRequest);
        }
    }

    private void initView(){
        back = (ImageView)findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        content = (TextView) findViewById(R.id.content);
        content.setMovementMethod(ScrollingMovementMethod.getInstance());
        time = (TextView) findViewById(R.id.time);
        title = (TextView) findViewById(R.id.title);
    }

    private void initData() {

        title.setText(Constants.getString(data.get("title")));
        content.setText("\u3000\u3000" + Constants.getString(data.get("content")));
        time.setText(Constants.getString(data.get("time")));
    }

}
